package com.prim.jmm;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * boolean 不适用于volatile的场景
 */
public class NoVolatile2 implements Runnable {

    volatile boolean done = false;

    AtomicInteger relaA = new AtomicInteger();

    @Override
    public void run() {
        for (int i = 0; i < 10000; i++) {
            done = !done;
            relaA.incrementAndGet();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        NoVolatile2 noVolatile = new NoVolatile2();
        Thread thread1 = new Thread(noVolatile);

        Thread thread2 = new Thread(noVolatile);
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
        System.out.println(noVolatile.done + ":" + noVolatile.relaA.get());
    }
}
